package final2107.night.day04;

import java.util.TreeSet;

public class ArrayOperationDemo {
    public static void main(String[] args) {
        int[] ary1 = {3,1,2};
        int[] ary2 = {4,3,5};
        //将数组转换为TreeSet集合,调用addAll()求并集
        TreeSet<Object> set1 = new TreeSet<>();
        TreeSet<Object> set2 = new TreeSet<>();

        for (Object obj:ary1)
            set1.add(obj);
        for (Object obj:ary2)
            set2.add(obj);

        //求并集
//        TreeSet<Object> sum = sum(set1,set2);
//        sum.forEach(e-> System.out.println(e));

        //求差集
        TreeSet<Object> mi = minus(set1,set2);
        mi.forEach(e-> System.out.print(e+" "));

    }

    //求差集
    public static TreeSet<Object> minus(TreeSet<Object> set1,TreeSet<Object> set2){
        //选择长度较短的集合
        TreeSet<Object> min = set1.size()<set2.size()?set1:set2;
        //获取较长的集合,让其保存差集元素
        TreeSet<Object> max = min==set1?set2:set1;
        //对min遍历,判断遍历到的元素是否存在于max中
        for (Object o:min) {
            if (max.contains(o)){
                max.remove(o);
            }else {
                max.add(o);
            }
        }
        return max;
    }

    public static TreeSet<Object> sum(TreeSet<Object> set1,TreeSet<Object> set2){
        set1.addAll(set2);
        return set1;
    }
}
